<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8"/>
    <title>教师管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <script src="layui-v2.5.4/layui.js"></script>
    <link rel="stylesheet" href="layui-v2.5.4/css/layui.css">
</head>
<body>

<div class="layui-row" style="padding: 15px;">

    <div id="searchDiv">
        <label>教师姓名：</label>
        <div class="layui-inline">
            <input class="layui-input" name="teacherName" id="teacherName" autocomplete="off" placeholder="模糊搜索">
        </div>
        <button class="layui-btn" id="searchBtn" style="margin-left: 10px;">搜索</button>
        <button class="layui-btn layui-btn-normal" id="resetBtn">重置</button>
    </div>

    <table id="teacherList" lay-filter="ccTb"></table>

</div>


<script type="text/html" id="tb_top">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delMore">批量删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>

<script type="text/html" id="tb_cell">
    <a class="layui-btn layui-btn-sm" lay-event="edit" title="编辑">编辑</a>
    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del" title="删除">删除</a>
</script>


<script>
    layui.use(['table','jquery','form'], function() {
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;

        table.render({
            elem: '#teacherList'
            ,height: 'full-80'
            ,page: true
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,limit: 15
            //,url: '/management/user/touserList.do'
            ,toolbar: '#tb_top'
            ,cols: [[
                {type:'checkbox'}
                ,{field: 'teacherID', title: '工号', sort: true,width:100}
                ,{field: 'name', title: '姓名'}
                ,{field: 'sex', title: '性别', sort: true,align:'center',
                         templet: function(d){
                             var sex = "";
                             if(d.sex=="1"){
                                 sex="男";
                             }else{
                                 sex="女";
                             }
                             return sex;
                         }
                }
                ,{field: 'nation', title: '民族',align:'center'}
                ,{field: 'workStatus', title: '在职状态',align:'center'}
                ,{field: 'birthday', title: '出生日期',sort: true}
                ,{field: 'addTime', title: '添加时间',sort: true,width:150,align:'center'}
                ,{field: 'dataSort', title: '排序',sort: true,align:'center'}
                ,{fixed: 'right', title: '操作',align:'center',width:150, toolbar: '#tb_cell'}
            ]]
            ,data: [{
                "id": "10001"
                ,"teacherID": "110"
                ,"name": "张老师"
                ,"sex": "1"
                ,"nation": "汉族"
                ,"workStatus": "在职"
                ,"birthday": "1968-5-20"
                ,"addTime": "2019-9-1 10:00:00"
                ,"dataSort":1
            }
            ,{
                "id": "10001"
                ,"teacherID": "120"
                ,"name": "李老师"
                ,"sex": "0"
                ,"nation": "汉族"
                ,"workStatus": "退休"
                ,"birthday": "1971-5-20"
                ,"addTime": "2019-9-2 10:00:00"
                ,"dataSort":2
                }
            ,{
                "id": "10003"
                ,"teacherID": "130"
                ,"name": "王老师"
                ,"sex": "0"
                ,"nation": "汉族"
                ,"workStatus": "病退"
                ,"birthday": "1973-5-20"
                ,"addTime": "2019-9-3 10:00:00"
                ,"dataSort":3
                }
            ]
        });



//监听表格头部工具栏事件
        table.on('toolbar(ccTb)', function(obj){
            switch(obj.event){
                case 'add':
                    layer.open({
                        type:2,
                        title:'添加信息',
                        area: ['600px','450px'],
                        fix: false,
                        maxmin: true,
                        shadeClose: true,
                        shade:0.4,
                        content:'toTeacherEdit.htm'
                    });
                    break;
                case 'delMore':
                    var checkStatus = table.checkStatus('teacherList');
                    var data = checkStatus.data;
                    var delList = [];
                    data.forEach(function(n,i){ //整理出id的数组
                        delList.push(n.id);
                    });
                    if (!isEmpty(delList)) {
                        layer.confirm('您确定删除'+data.length+'条数据吗？', function (index) {
                            $.post("/management/teacher/delMoreTeacher.do", {'delList': delList}, function (res) {
                                if (res == 200){
                                    layer.msg('批量删除成功', { icon: 1, time: 800, shade: 0.3}, function () {
                                        //重载表格
                                        table.reload('teacherList');
                                        //更新树，也是重载数据
                                        //loadTree($("#curTid").val());
                                    });
                                }
                            });
                        });
                    }else{
                        layer.msg('请选择需要删除的行', {icon: 5, time: 800, shade: 0.3});
                    }
                    break;
            };
        });

        //监听表格行工具按钮事件
        table.on('tool(ccTb)', function(obj){
            switch(obj.event){
                case 'edit':
                    layer.open({
                        type:2,
                        title:'编辑信息',
                        area: ['600px','450px'],
                        fix: false,
                        maxmin: true,
                        shadeClose: true,
                        shade:0.4,
                        content:'toTeacherEdit.htm?&id='+obj.data.id
                    });
                    break;
                case 'del':
                    layer.confirm('您确定删除“'+obj.data.userName+'”吗？', function(index){
                        $.post("/management/teacher/delTeacher.do", { id: obj.data.id }, function (res) {
                            if (res == 200) {//删除成功，刷新当前页表格
                                layer.msg('删除成功', { icon: 1, time: 800, shade: 0.3}, function () {
                                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                    layer.close(index);
                                    //重载左侧树，树节点id，是当前表格行数据的父id
                                    // loadTree(obj.data.parentId);
                                });
                            }else if(res == 300){//删除失败
                                layer.msg('删除失败', { icon: 2, time: 800, shade: 0.3},function () {
                                    layer.close(index);
                                });
                            }
                        });
                    });
                    break;
            };
        });

        //搜索表格
        $('#searchBtn').on('click', function(){
            var userName = $("#userName").val();
            //重载右侧表格
            table.reload('teacherList', {
                where: {
                    'queryParameter.teacherName_li': userName
                }
            });
        });

        //重置表格
        $('#resetBtn').on('click', function(){
            $("#userName").val("");
            //重载右侧表格
            table.reload('teacherList', {
                where: {
                    'queryParameter.teacherName_li': ''
                }
            });
        });

    });
</script>
</body>
</html>

